package selfLearn.theList.oneWayList;

import selfLearn.theList.toWayList.TwoLinkList;

import java.util.Iterator;

/**
 * 描述:
 *
 * @Package: selfLearn.theList.oneList
 * @ClassName: LinkList
 * @Author: 陈威
 * @CreateTime: 2022/1/27 11:25
 * @Description: 单向链表工具类
 */
class LinkList<T> implements Iterable<T> {
    //记录头节点
    private Node head;
    //记录链表长度
    private int N;
    //尾巴节点
    private Node tailNode;


    private class Node {
        T item;
        private Node next;

        public Node(T t, Node next) {
            this.item = t;
            this.next = next;
        }

        public Node() {
        }

    }

    public LinkList() {
        //初始化头节点
        this.head = new Node(null, null);
        //初始化元素个数
        this.N = 0;

        //初始化尾巴节点
        this.tailNode = head;
    }

    //清空链表
    public void clearList() {
        this.head.next = null;
        this.N = 0;
    }

    public int length() {
        return N;
    }

    //判断链表是否为空
    public Boolean isEmpty() {
        return N == 0;
    }

    //获取指定位置i处的元素
    public T get(int index) {
        if (index >= length()) {
            return null;
        }
        //通过循环遍历,遍历index次，找到该元素然后返回
        Node target = head.next;
        for (int i = 0; i < index; i++) {
            target = target.next;
        }
        return target.item;
    }

    //插入节点  //尾节点插入法
    public void insert(T t) {
/*        //找到最后一个节点
        Node target = head;
        while (target.next != null) {
            target = target.next;
        }*/
        //创建新节点
        Node newNode = new Node(t, null);
        //让最后一个节点指向新节点
        tailNode.next = newNode;
        tailNode = tailNode.next;
        N++;

    }

    public Boolean isBeyond(Integer index) {
        return index <= length() ? true : false;
    }

    //向指定位置index插入节点
    public void insert(int index, T t) {
/*        if(index>=N){
            return "没有这么的";
        }*/
        if (!isBeyond(index)) {
            System.out.println("您好，链表没有这么长的索引呢 链表长度=" + N);
            return;
        }
        //找到index位置前一个节点
        Node pre = head;
        for (int i = 0; i < index - 1; i++) {
            pre = pre.next;
        }
        //找到index位置节点
        Node currentNode = pre.next;
        //创建新的节点 新节点要指向原来的节点
        Node newNode = new Node(t, currentNode);
        //前一个节点指向要插入的节点
        pre.next = newNode;
        N++;
    }

    //删除指定位置index处的节点，并返回该节点
    public Node remove(int index) {
        if (!isBeyond(index)) {
            System.out.println("您好，链表没有这么长的索引呢 链表长度=" + N);
            return null;
        }
        Node target = head;
        for (int i = 0; i < index - 1; i++) {
            target = target.next;
        }
        Node temp = target.next;
        target.next = target.next.next;
        N--;
        return temp;
    }


    //获取元素在链表中第一次出现的位置
    public Integer getIndex(T t) {
        if (isEmpty()) {
            System.out.println("您好，链表为空");
            return -1;
        }
        Node target = head;
        for (int i = 0; target.next != null; i++) {
            if (target.next.item.equals(t)) {
                return i;
            }
            target = target.next;
        }
        return -1;
    }

    //反转链表
    public void reverse() {
        if(head.next==null){
            return;
        }
        Node prev = null;
        Node cur = head.next;
        //prev存储的是上一个节点
        while (cur != null) {
            //先定义一个节点存储当前节点的下一个节点
            Node next = cur.next;
            //将当前节点指向上一轮存储的上一个节点
            cur.next = prev;
            //
            prev = cur;
            cur = next;
        }
        head.next = prev;
    }


    @Override
    public Iterator<T> iterator() {
        return new Literator();
    }

    private class Literator implements Iterator {
        private Node n;

        public Literator() {
            this.n = head;

        }

        @Override
        public boolean hasNext() {
            return n.next != null;
        }

        @Override
        public Object next() {
            n = n.next;
            return n.item;
        }
    }
}
